import os

from starlette.staticfiles import StaticFiles
from fastapi.openapi.docs import get_swagger_ui_html
from fastapi import FastAPI, HTTPException
from mian import CONFIG_DIR
from mian.routes import UploadRoute

class Control:
    """
     控制总端g
    """

    def __init__(self):
        static_dir=os.path.join(CONFIG_DIR, 'static')
        if not os.path.isdir(static_dir):
            raise FileNotFoundError(f"静态目录不存在: {static_dir}")
        self.app = FastAPI(title='模板', docs_url=None, description='模板api')
        self.app.mount("/static", StaticFiles(directory=static_dir), name='static')
        self.app.add_api_route(path="/swagger", endpoint=self.swagger_ui, methods=["GET"], include_in_schema=False)
        UploadRoute().uploadRoute(self.app)

    #swager 文档
    async def swagger_ui(self):
        if not self.app.openapi():  # 确保 OpenAPI 定义存在
            raise HTTPException(status_code=500, detail="OpenAPI 定义未生成")
        return get_swagger_ui_html(
            openapi_url=self.app.openapi_url,
            title=self.app.title + "-swagger",
            swagger_js_url="/static/swagger-ui-bundle.js",
            swagger_css_url="/static/swagger-ui.css",
        )



# if __name__ == '__main__':
#     path = os.path.dirname(os.path.dirname(__file__))
#     print(path)
